<template>
  <div>
    <h1>管理员列表</h1>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="id" width="60" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="nickname" label="昵称"></el-table-column>
      <el-table-column prop="phone" label="手机号码"></el-table-column>
      <el-table-column prop="email" label="电子邮箱"></el-table-column>
      <el-table-column prop="description" label="简介" width="180" header-align="center"></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="80" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="handleChangeEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    handleChangeEnable(admin) {
      console.log('即将把id=' + admin.id + '的管理员的启用状态值改为：' + admin.enable);
      let url = 'http://localhost:9081/admins/' + admin.id;
      let enableText = ['禁用', '启用'];
      if (admin.enable == 1) {
        // 发出“启用”的请求
        url += '/enable';
      } else {
        // 发出“禁用”的请求
        url += '/disable';
      }
      console.log('url = ' + url);
      this.axios
        .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
        .post(url)
        .then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            this.$message({
              message: enableText[admin.enable] + '【' + admin.username + '】成功！',
              type: 'success'
            });
          } else {
            this.$message.error(responseBody.message);
            this.loadAdminList();
          }
        });
    },
    openDeleteConfirm(admin) {
      this.$confirm('即将永久删除【' + admin.username + '】管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(admin.id);
      }).catch(() => {
      });
    },
    handleEdit(id) {
      console.log('准备【编辑管理员】，数据id为【' + id + '】……');
    },
    handleDelete(id) {
      console.log('准备【删除管理员】，数据id为【' + id + '】……');
      let url = 'http://localhost:9081/admins/' + id + '/delete';
      console.log('url = ' + url);
      this.axios
        .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
        .post(url)
        .then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            console.log('删除成功！');
            this.$message({
              message: '删除成功！',
              type: 'success'
            });
          } else if (responseBody.state == 40400) {
            console.log(responseBody.message);
            this.$message.error(responseBody.message);
          } else {
            console.log(responseBody.message);
            this.$message.error(responseBody.message);
            return;
          }
          this.loadAdminList();
        })
    },
    loadAdminList() {
      console.log('准备从服务器端加载【管理员列表】……');
      let url = 'http://localhost:9081/admins';
      console.log('请求路径：' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url)
          .then((response) => {
            let responseBody = response.data;
            console.log('接收到服务器端响应的结果：');
            console.log(responseBody);
            this.tableData = responseBody.data;
          });
    }
  },
  mounted() {
    console.log('mounted');
    this.loadAdminList();
  }
}
</script>

<style scoped>

</style>